package com.rimi.lzhg.controller;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.rimi.lzhg.entry.User;
import com.rimi.lzhg.listener.ExcelReaderListener;
import com.rimi.lzhg.response.ResponseEntity;
import com.rimi.lzhg.service.OrderService;
import com.rimi.lzhg.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @DeleteMapping("/manager/user")
    public Object deleteUserById(Integer id){
        boolean b = userService.deleteUserById(id);
        if(b){
            return ResponseEntity.success("用户信息删除成功");
        }
        return ResponseEntity.error("用户信息失败");
    }


    @PostMapping("/manager/excel")
    public Object readExcel(@RequestParam MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseEntity.error("上传失败，请选择文件");
        }
        InputStream inputStream = file.getInputStream();
        // 解析每行结果在listener中处理
        AnalysisEventListener listener = new ExcelReaderListener(userService);
        ExcelReader excelReader = new ExcelReader(inputStream,null, listener);
        excelReader.read(new Sheet(1, 1, User.class));
        inputStream.close();
        return ResponseEntity.success("导入成功,用户默认密码:123456");
    }

    @GetMapping("/manager/excel")
    public void outExcel(HttpServletResponse response) throws UnsupportedEncodingException {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置导出文件名，并编码
        String fileName = "导入用户信息模板."+ExcelTypeEnum.XLSX;
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ExcelWriter excelWriter=new ExcelWriter(outputStream,ExcelTypeEnum.XLSX);
            Sheet sheet = new Sheet(1, 0,User.class);
            excelWriter.write(null,sheet);
            excelWriter.finish();
            if(outputStream!=null) {
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "用户列表",notes = "根据学号以及密码进行登录")
    @GetMapping("/manager/user")
    public Object getUserList(){
        List<User> userList = userService.getUserList(null);
        return ResponseEntity.success(userList.size(),userList);
    }

    @ApiOperation(value = "用户登录",notes = "根据学号以及密码进行登录")
    @PostMapping("/login")
    public Object login(User user){
        if(user==null){
            return false;
        }
        //用户名不存在
        if(userService.getUserByCode(user.getCode())==null){
            return 2;
        }
        User login = userService.login(user);
        //用户名或密码错误
        if (login==null){
            return 3;
        }
        return login;
    }

    @ApiOperation(value = "用户注册",notes = "输入学号与密码进行注册")
    @PostMapping("/register")
    public Object register(User user){
        return userService.register(user);
    }


    @ApiOperation(value = "用户信息修改",notes = "根据用户ID进行修改用户信息，为空则不修改保持不变")
    @PutMapping("/user")
    public Object update(User user){
        return userService.updateUser(user);
    }

}
